﻿using System;
using System.IO;
using Newtonsoft.Json.Linq;
namespace CoinsLibrary
{
    public class ConfigHelper
    {
        static string filePath = "config";
        public JObject KeyValue;
        public ConfigHelper()
        {
            try
            {
                var json = File.ReadAllText(filePath);
                this.KeyValue = JObject.Parse(json);
            }
            catch
            {
                throw new Exception("读取配置文件错误");
            }
        }
        public string GetConfig(string key)
        {
            try
            {
                return this.KeyValue[key].ToString();
            }
            catch
            {

            }
            return "";
        }
        public void SetConfig(string key, string value, bool save = false)
        {
            this.KeyValue[key] = value;
            if (save)
            {
                this.Save();
            }
        }
        public void Save()
        {
            File.WriteAllText(filePath, this.KeyValue.ToString());
        }
    }
}
